package Humburg.Servlect;


import Humburg.service.HumburgServiceImpl;
import Snacks.entiy.Snacks;
import util.UtilYun;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

@WebServlet(name = "HumburgServletAdd", value = "/HumburgServletAdd")
@MultipartConfig()
public class HumburgServletAdd extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String name = request.getParameter("name");
        String description = request.getParameter("description");
        String price = request.getParameter("price");
        double priceidnw = price != null ? Double.parseDouble(price) : 0.00;
        String huodonprice = request.getParameter("huodonprice");
        double huodonpriceidnw = huodonprice != null ? Double.parseDouble(huodonprice) : 0.00;
        String feature = request.getParameter("feature");

        HumburgServiceImpl ser = new HumburgServiceImpl();
        String status = request.getParameter("status");
        int statusid = status != null ? Integer.parseInt(status) : 0;
        String statusyu = request.getParameter("statusyu");
        String statusyuline = request.getParameter("statusyuline");
        int statusyulinename = statusyuline != null ? Integer.parseInt(statusyuline) : 0;

        Snacks snacs = new Snacks();
        snacs.setSnacksName(name);
        snacs.setSnacksPtion(description);
        snacs.setSnacksPrice(priceidnw);
        snacs.setSnacksYhPrice(huodonpriceidnw);
        snacs.setSnackFeature(feature);
        snacs.setShelf(statusid);
        snacs.setHaoneid(statusyulinename);

        Part part = request.getPart("uFile");
        Map<String, String> map = UtilYun.uploadImage(part);
        String realFile = map.get("realFile");
        String dummyFile = map.get("dummyFile");
        String suf = map.get("suf");
        String uuid = map.get("uuid");
        snacs.setTupian(uuid);
        snacs.setHouzui(suf);

        PrintWriter out = response.getWriter();
        int in = ser.addInfor(snacs);


        if (in > 0) {
            out.println("<script> alert(\"添加成功\") </script>");
            response.sendRedirect("Hamburg.jsp");
        } else {
            out.print("<script>alert(\"添加失败\")</script>");
            response.sendRedirect("HamburgTj.jsp");
        }
    }
}
